/*
 * Copyright (c) 2018.
 * 北京九思金信科技有限公司对该文件内容保留所有权利。
 * 未经许可，不得私自拷贝、传播、发布、引用该文件内容，违者将被追究法律责任。
 */

import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/login/Login.vue'
import Nav from '@/components/JoysHeader/JoysHeader.vue'

import Home from '@/views/nav/home/Home.vue'
import Info from '@/views/nav/info/Info.vue'
import Sue from '@/views/nav/sue/Sue.vue'
import My from '@/views/nav/my/My.vue'
import Iframe from '@/views/nav/iframe/Iframe.vue'

// // home
import HomeIndex from '@/views/nav/home/component/HomeIndex.vue'
import Task from '@/views/nav/home/component/Task.vue'
import TaskItem from '@/views/nav/home/component/TaskItem.vue'

// Info
import InfoIndex from '@/views/nav/info/component/InfoIndex.vue'
import InfoList from '@/views/nav/info/component/InfoList.vue'
import InfoSearch from '@/views/nav/info/component/InfoSearch.vue'
import InfoTask from '@/views/nav/info/component/InfoTask.vue'

// my
import MyIndex from '@/views/nav/my/component/MyIndex.vue'
import MySet from '@/views/nav/my/component/MySet.vue'
import MyRegards from '@/views/nav/my/component/MyRegards.vue'
import MySolve from '@/views/nav/my/component/MySolve.vue'

// sue
import SueIndex from '@/views/nav/sue/component/SueIndex.vue'
import SueSearch from '@/views/nav/sue/component/SueSearch.vue'

// iframe页面
import IframeIndex from '@/views/nav/iframe/component/IframeIndex.vue'

// 应用app
// 客户管理
import CustomerManagement from '@/views/apply/customerManagement/CustomerManagement'
import CmIndex from '@/views/apply/customerManagement/component/CmIndex'
import CmAddIndex from '@/views/apply/customerManagement/component/CmAddList'

// 报价测算
import QuoteCalculate from '@/views/apply/quoteCalculate/QuoteCalculate'
import QuoteCalculateIndex from '@/views/apply/quoteCalculate/component/QuoteCalculateIndex'
import QuoteCalculateResult from '@/views/apply/quoteCalculate/component/QuoteCalculateResult'
import QuoteCalculateIntroduce from '@/views/apply/quoteCalculate/component/QuoteCalculateIntroduce'

Router.prototype.goBack = function() {
  this.isBack = true
  window.history.go(-1)
}

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '*', component: Home },
    { path: '/', redirect: '/home' },
    { path: '/login', name: 'login', component: Login },
    { path: '/nav', name: 'nav', component: Nav, redirect: '/Home' },

    {
      path: '/iframe',
      name: 'iframe',
      component: Iframe,
      children: [{ path: '', name: 'iframeIndex', component: IframeIndex }]
    },
    {
      path: '/home',
      name: 'home',
      component: Home,
      children: [
        { path: '', name: 'homeIndex', component: HomeIndex },
        { path: 'task', name: 'task', component: Task },
        { path: ':id', name: 'taskItem', component: TaskItem }
      ]
    },
    {
      path: '/info',
      name: 'info',
      component: Info,
      children: [
        { path: '', name: 'infoIndex', component: InfoIndex },
        { path: 'infoSearch', name: 'infoSearch', component: InfoSearch },
        { path: ':id', name: 'infoList', component: InfoList }
      ]
    },
    {
      path: '/sue',
      name: 'sue',
      component: Sue,
      children: [
        { path: '', name: 'sueIndex', component: SueIndex },
        { path: 'sueSearch', name: 'sueSearch', component: SueSearch }
      ]
    },
    {
      path: '/my',
      name: 'my',
      component: My,
      children: [
        { path: '', name: 'myIndex', component: MyIndex },
        { path: 'mySet', name: 'mySet', component: MySet },
        { path: 'myRegards', name: 'myRegards', component: MyRegards },
        { path: 'mySolve', name: 'mySolve', component: MySolve }
      ]
    },
    // 应用item
    {
      // 客户管理
      path: '/customerManagement',
      name: 'customerManagement',
      component: CustomerManagement,
      children: [
        { path: '', name: 'cmIndex', component: CmIndex },
        { path: 'cmAddIndex', name: 'cmAddIndex', component: CmAddIndex }
      ]
    },
    {
      // 报价测算
      path: '/quoteCalculate',
      name: 'quoteCalculate',
      component: QuoteCalculate,
      children: [
        {
          path: '',
          name: 'quoteCalculateIndex',
          component: QuoteCalculateIndex
        },
        {
          path: 'quoteCalculateResult',
          name: 'quoteCalculateResult',
          component: QuoteCalculateResult
        },
        {
          path: 'quoteCalculateIntroduce',
          name: 'quoteCalculateIntroduce',
          component: QuoteCalculateIntroduce
        }
      ]
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    return { x: 0, y: 0 }
  }
})

// 配置路由的拦截器
router.beforeEach((to, from, next) => {
  // 如果访问登录的路由地址
  if (to.name === 'login') {
    next()
  } else if (to.name === 'quoteCalculateIndex') {
    next({})
  } else {
    // 如果请求的不是登录页面，验证token
    //  获取本地存储中的token
    const token = localStorage.getItem('AUTH-TOKEN')
    if (!token) {
      // 如果没有token，跳转到登录
      next({
        name: 'login'
      })
    } else {
      // 如果有token，继续往下执行
      next()
    }
  }
})

export default router
